﻿//-----------------------------------------------------------------------
// <copyright file="DBHelper.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings
using System.Data.EntityClient;
using FastNET.CleanAct.Client.ManagerEngineData;
using FastNET.CleanAct.Client.ManagerEngineData.Entities;
using FastNET.CleanAct.Client.ManagerEngineData.Properties;
using Action = FastNET.CleanAct.Client.ManagerEngineData.Entities.Action;
#endregion

namespace FastNET.CleanAct.Testing.Client.ManagerEngineData.Helpers
{
    /// <summary>
    /// Internal class designed for managing database state operations
    /// </summary>
    internal static class DBHelper
    {
        #region Constructors

        /// <summary>
        /// Static constructor for DBHelper
        /// </summary>
        static DBHelper()
        {
            Provider = new SQLDataProvider();
        }

        #endregion

        #region Static Properties

        /// <summary>
        /// Gets a static reference to the SQL Repository Provider for helper functions
        /// </summary>
        public static SQLDataProvider Provider { get; private set; }

        #endregion

        #region Static Methods

        /// <summary>
        /// Creates the general connection for connecting to the data store
        /// </summary>
        /// <returns>Entity connection for connecting to the Entity store</returns>
        public static EntityConnection CreateConnection()
        {
            EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder
            {
                Metadata = "res://*/Entities.ManagerEngineModel.csdl|res://*/Entities.ManagerEngineModel.ssdl|res://*/Entities.ManagerEngineModel.msl",
                Provider = "System.Data.SqlServerCe.3.5",
                ProviderConnectionString = Settings.Default.ManagerEngineConnectionString
            };

            return new EntityConnection(builder.ConnectionString);
        }

        /// <summary>
        /// Removes all data from the database
        /// </summary>
        public static void ClearDatabase()
        {
            using (ManagerDataEntities dataSource = new ManagerDataEntities(CreateConnection()))
            {
                foreach (Log item in dataSource.Logs)
                {
                    dataSource.DeleteObject(item);
                }

                foreach (ScheduleHistory item in dataSource.ScheduleHistory)
                {
                    dataSource.DeleteObject(item);
                }

                foreach (Schedule item in dataSource.Schedules)
                {
                    dataSource.DeleteObject(item);
                }

                foreach (Action item in dataSource.Actions)
                {
                    dataSource.DeleteObject(item);
                }

                foreach (TemplateGroup item in dataSource.TemplateGroups)
                {
                    dataSource.DeleteObject(item);
                }

                foreach (Template item in dataSource.Templates)
                {
                    dataSource.DeleteObject(item);
                }

                foreach (Package item in dataSource.Packages)
                {
                    dataSource.DeleteObject(item);
                }

                dataSource.SaveChanges();
            }
        }

        #endregion
    }
}
